package com.can.blockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

//阻塞队列
public class ArrayBlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        Test4();
    }

    /**
     *  抛出异常
     */
    public static void Test1(){
        //队列的大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
        System.out.println(arrayBlockingQueue.element());//查看队首元素是谁
        //java.lang.IllegalStateException: Queue full 抛出异常
//        System.out.println(arrayBlockingQueue.add("d"));
        System.out.println("===========");
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        //java.util.NoSuchElementException 抛出异常
//        System.out.println(arrayBlockingQueue.remove());
    }

    /**
     *有返回值，没有异常
     */
    public static void Test2(){
        //队列的大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.peek());//查看队首元素是谁 null 不抛出异常
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("d"));  //false 不抛出异常!
        System.out.println("==========");
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());//返回null  也不抛出异常！
    }

    /**
     * 等待，阻塞(一直等待)
     */
    public static void Test3() throws InterruptedException {
        //队列多小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);

        //一直阻塞
        arrayBlockingQueue.put("a");//没有返回值
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
        //arrayBlockingQueue.put("d"); //队列没有位置了，它会一直等待
        System.out.println("=========");
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());//没有这个元素，一直阻塞
    }

    /**
     * 等待，阻塞(等待超时)
     */
    public static void Test4() throws InterruptedException {
        //队列大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        arrayBlockingQueue.offer("a");
        arrayBlockingQueue.offer("b");
        arrayBlockingQueue.offer("c");
        //offer(值,时间,时间单位)
        //arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS);  //如果前面是满，那么久等待2秒，如果还是没有位置，就超时退出
        System.out.println("============");
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        //poll(时间,时间单位)
        System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS)); //等待超过两秒就退出

    }
}
